import {
  createRouter,
  createWebHashHistory
} from 'vue-router'

const routes = [{
  name: 'Error404',
  path: '/:error*',
  component: () => import('@/pages/error/404.vue'),
  meta: {
    isLogin: false
  }
},
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/pages/login/index.vue'),
    meta: {
      isLogin: false
    }
  },
  {
    path: '/',
    redirect: '/overview',
    component: () => import('@/pages/index/index.vue'),
    children: [
      {
        path: '/overview',
        name: 'Overview',
        component: () => import('@/pages/overview/index.vue')
      },
      {
        path: '/overview/addGoods',
        name: 'AddGoods',
        component: () => import('@/pages/overview/addGoods.vue')
      },
      {
        path: '/overview/details/:id',
        name: 'Details',
        component: () => import('@/pages/overview/details.vue')
      },
      {
        path: '/overview/edit/:id',
        name: 'Edit',
        component: () => import('@/pages/overview/edit.vue')
      },
      {
        path: '/overview/goodsDetail/:id',
        name: 'GoodsDetail',
        component: () => import('@/pages/overview/goodsDetail.vue')
      },
      {
        path: '/staff',
        name: 'Staff',
        component: () => import('@/pages/staff/index.vue')
      },
      {
        // operateLog
        path: '/wareStaff',
        name: 'WareStaff',
        component: () => import('@/pages/wareStaff/index.vue')
      },
      {
        path: '/operateLog',
        name: 'OperateLog',
        component: () => import('@/pages/operateLog/index.vue')
      }
    ]
  }
]
const router = createRouter({
  history: createWebHashHistory(),
  routes
})
router.beforeEach((to, from, next) => {
  const toPath = to.path
  const token = localStorage.getItem('_antiFake_token')
  if (toPath === '/login') {
    if (token) {
      next({ name: 'Overview' })
    } else {
      next()
    }
  } else {
    if (token) {
      next()
    } else {
      next({ name: 'Login' })
    }
  }
})
export default router
export {
  routes
}
